package org.smr.ministore.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.blade.dao.base.BaseMapper;
import org.blade.msg.ErrorCode;
import org.blade.utils.IdGenUtils;
import org.blade.utils.Pager;
import org.blade.service.base.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smr.ministore.dao.GoodsPriceMapper;
import org.smr.ministore.dao.GoodsPriceOrganMapper;
import org.smr.ministore.entities.GoodsPrice;
import org.smr.ministore.entities.GoodsPriceOrgan;
import org.smr.ministore.service.GoodsPriceService;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("GoodsPriceService")
@Transactional(readOnly = false)
public class GoodsPriceServiceImpl extends AbstractService<Long, GoodsPrice> implements GoodsPriceService {


    private static Logger logger = LoggerFactory.getLogger(GoodsPriceServiceImpl.class);


	@Autowired
	private GoodsPriceMapper mapper;

    @Autowired
    private GoodsPriceOrganMapper goodsPriceOrganMapper;

    @Autowired
    public GoodsPriceServiceImpl(GoodsPriceMapper mapper) {
        super(mapper);
    }

    public int save(GoodsPrice entity) {
		super.saveAttatch(entity);
        GoodsPriceOrgan goodsPriceOrgan = this.covert(entity);
        IdGenUtils.saveAttatch(goodsPriceOrgan);
        goodsPriceOrganMapper.insert(goodsPriceOrgan);
        return mapper.insert(entity);
	}

	private GoodsPriceOrgan covert(GoodsPrice entity) {
        GoodsPriceOrgan goodsPriceOrgan = new GoodsPriceOrgan();
        goodsPriceOrgan.setOrganId(entity.getOrganId());
        goodsPriceOrgan.setOrganPath(entity.getOrganPath());
        goodsPriceOrgan.setGoodsPriceId(entity.getId());
        goodsPriceOrgan.setTenantId(entity.getTenantId());
        goodsPriceOrgan.setCostPrice(entity.getCostPrice());
        goodsPriceOrgan.setDiscountPrice(entity.getDiscountPrice());
        goodsPriceOrgan.setPrice(entity.getPrice());
        goodsPriceOrgan.setSuggestedRetailPrice(entity.getSuggestedRetailPrice());
        return goodsPriceOrgan;
    }



	public int updateById(GoodsPrice id) {
		super.modifyAttach(id);

        GoodsPriceOrgan goodsPriceOrgan = this.covert(id);
        IdGenUtils.modifyAttach(goodsPriceOrgan);
        this.goodsPriceOrganMapper.updateById(goodsPriceOrgan);
		return mapper.updateById(id);
	}

    @Transactional(readOnly = true)
	public GoodsPrice queryById(Long id) {
		return mapper.selectById(id);
	}

	public int deleteById(Long id) {
		return mapper.deleteById(id);
	}

    @Transactional(readOnly = true)
    public Pager<GoodsPrice> paginate( GoodsPrice entity, Long pageSize, Long pageNum) {

		Pager<GoodsPrice> pager = new Pager( pageSize, pageNum );
		List list  = mapper.pagination(entity, pager);
		pager.setEntities(list);
		return pager;
    }
	
	public int batchSave(List<GoodsPrice> entities) {
        this.batchSaveAttatch(entities);
		return mapper.batchInsert(entities);
	}


    public int batchModify(List<GoodsPrice> entities) {
        this.batchModifyAttatch(entities);
        return mapper.batchUpdate(entities);
	}


    public int deleteByIdLogical(Long id) {

        GoodsPrice goodsPrice = this.queryById(id);
        GoodsPriceOrgan goodsPriceOrgan = new GoodsPriceOrgan();
        goodsPriceOrgan.setGoodsPriceId(id);
        goodsPriceOrgan.setTenantId(goodsPrice.getTenantId());
        goodsPriceOrgan.setOrganPath(goodsPrice.getOrganPath());
        int quantityOfRef = this.goodsPriceOrganMapper.queryQuantityOfRef(goodsPriceOrgan);
        if (quantityOfRef > 1) { //有一个引用以上
            return ErrorCode.Persistence.NONE_EFFECT;
        }
        this.goodsPriceOrganMapper.deleteByGoodsPrice(goodsPriceOrgan);

        return mapper.deleteByIdLogical(id);
    }



    @Transactional(readOnly = true)
    public Pager<Map<String, Object>> getGoodsPriceList(Long organId,
                                                       Long typeId,
                                                       Long brandId,
                                                       String goodsName,
                                                       Integer quote,
                                                       Long pageSize,
                                                       Long pageNum) {
        Pager<Map<String, Object>> pager = new Pager( pageSize, pageNum );
        pager.setEntities(this.mapper.getGoodsPricePagination( organId,  typeId,  brandId,  goodsName,  quote, pager));
		return pager;
	}

    @Transactional(readOnly = true)
    public Pager<Map<String, Object>> getGoodsPriceList(Map<String, Object> params, Long pageSize, Long pageNum) {

        Long organId = MapUtils.getLong(params,"organId");
        Long typeId  = MapUtils.getLong(params,"typeId");
        Long brandId  = MapUtils.getLong(params,"brandId");
        String goodsName  = MapUtils.getString(params,"goodsName");
        int quote  = MapUtils.getIntValue(params,"quote");
        Pager<Map<String, Object>> pager = new Pager( pageSize, pageNum );
        pager.setEntities(this.mapper.getGoodsPricePagination( organId,  typeId,  brandId,  goodsName,  quote, pager));
        return pager;
    }

    public GoodsPrice getGoodsPriceBy(Long organId, Long goodsId) {
        return this.mapper.getGoodsPriceBy(organId, goodsId);
    }

    public Long getId() {
        return IdGenUtils.getNextLongId();
    }
}
